package com.onlineMIS.filter;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.onlineMIS.ORM.entity.chainS.report.ChainFinanceReportItem;
import com.onlineMIS.common.loggerLocal;

public class SystemParm {

	public static final Properties SystemParm = new Properties();
	
	public static void load() {
		try {
			InputStream is = SystemParm.class.getClassLoader()
					 .getResourceAsStream("sysParms/SystemParm.properties");
			
			SystemParm.load(is);
			
			
		} catch (IOException e) {
			loggerLocal.error(e);
		}
	}
	
	public static String getParm(String parmName){
		String value = SystemParm.getProperty(parmName);
		if (value == null)
			value = "";
		return value.trim();
	}
	
	public static int getTestSupplierId(){
		String TEST_ID_OBJ = getParm("TEST_SUPPLIER_ID");
		if (!StringUtils.isEmpty(TEST_ID_OBJ)){
			int TEST_ID = Integer.parseInt(TEST_ID_OBJ);
			return TEST_ID;
		} else 
			return -1;
	}
	
	public static int getTestClientId(){
		String TEST_ID_OBJ = getParm("TEST_CLIENT_ID");
		if (!StringUtils.isEmpty(TEST_ID_OBJ)){
			int TEST_ID = Integer.parseInt(TEST_ID_OBJ);
			return TEST_ID;
		} else 
			return -1;
	}
	
	public static int getTestChainId(){
		String TEST_ID_OBJ = getParm("TEST_CHAIN_ID");
		if (!StringUtils.isEmpty(TEST_ID_OBJ)){
			int TEST_ID = Integer.parseInt(TEST_ID_OBJ);
			return TEST_ID;
		} else 
			return -1;
	}
	
	public static String getPYSCategoryId(){
		String TEST_ID_OBJ = getParm("PYS_CATEGORY");
		if (!StringUtils.isEmpty(TEST_ID_OBJ)){
			String[] idArray = TEST_ID_OBJ.split(",");
			String arrayResult = "(";
			for (String ele: idArray) 
				arrayResult += ele + ",";
			arrayResult += "-1)";
			return arrayResult;
		} else 
			return "(-1)";
	}
	
	public static Set<Integer> getCategoryIdSet() {
		Set<Integer> idSet = new HashSet<Integer>();
		
		String TEST_ID_OBJ = getParm("PYS_CATEGORY");
		if (!StringUtils.isEmpty(TEST_ID_OBJ)){
			String[] idArray = TEST_ID_OBJ.split(",");

			for (String ele: idArray) {
				if (StringUtils.isBlank(ele))
					continue;
				idSet.add(Integer.parseInt(ele));
			}

			return idSet;
		} else {
			idSet.add(-1);
			return idSet;
		}
	}
	

	/**
	 * 获取图片路径
	 * @return
	 */
	public static String getQxPicURLHead() {
		return getParm("QX_PIC_URL");
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public static int getQXCategory(){
		String value = SystemParm.getProperty("QX_PURCHASE_CATEGORY_ID");
		if (StringUtils.isEmpty(value))
			return 0;
		return Integer.parseInt(value);
	}

		/**
	 * 获取折扣品牌
	 * @return
	 */
	public static Double getSpecialBrand(Integer brandId) {
		Map<Integer, Double> map = new HashMap<Integer, Double>();
		
		String specialBrand = getParm("SPECIAL_BRAND_ID");
		if (specialBrand != null && specialBrand != "") {
			String brand[] = specialBrand.split(",");
			for (String brandInfo : brand) {
				if (StringUtils.isBlank(brandInfo) || brandInfo.indexOf(":")!=-1)
					continue;
				Integer brandInfoId = Integer.valueOf(brandInfo.substring(0,brandInfo.indexOf(":")));
				double discount = Double.valueOf(brandInfo.substring(brandInfo.indexOf(":")+1,brandInfo.length()));
				map.put(brandInfoId,discount);
			}
		}
		
		if (map.get(brandId) != null) {
			return map.get(brandId);
		}else {
			return 0d;
		}				
		
	}

	public static List<Integer> getCanHideOrderRoleIds(){
		String idString = getParm("CAN_HIDE_ROLE_ID");
		if (StringUtils.isEmpty(idString)){
//			int TEST_ID = Integer.parseInt(TEST_ID_OBJ);
			return new ArrayList<Integer>();
		} else {
			List<Integer> list = new ArrayList<Integer>();
			String[] valueArray = idString.split(",");
			for (String value2 : valueArray){
				if (StringUtils.isBlank(value2))
					continue;
				list.add(Integer.parseInt(value2));
			}
			return list;
		}
	}

}
